﻿using System.Collections.Generic;
using System.Linq;
using DTO;

namespace DAO
{
    public class LoaiQuangCaoDAO
    {
        public static List<LoaiQuangCaoDTO> getAll()
        {
            List<LoaiQuangCaoDTO> result = new List<LoaiQuangCaoDTO>();
            RaoVatDataContext dbRaoVat = new RaoVatDataContext();

            var query = dbRaoVat.LOAIQUANGCAOs.Where(dm => dm.Deleted.Equals(0)).ToList();

            foreach (var ds in query)
            {
                LoaiQuangCaoDTO dto = new LoaiQuangCaoDTO();

                dto.MaLoaiQuangCao = ds.MaLoaiQuangCao;
                dto.TenLoaiQuangCao = ds.TenLoaiQuangCao;

                result.Add(dto);
            }

            return result;
        }

        public static LoaiQuangCaoDTO getAlongCode(int maLoai)
        {
            LoaiQuangCaoDTO result = new LoaiQuangCaoDTO();
            RaoVatDataContext dbRaoVat = new RaoVatDataContext();

            var query = dbRaoVat.LOAIQUANGCAOs.Where(dm => dm.Deleted.Equals(0) &&
                dm.MaLoaiQuangCao == maLoai).Single();

            result.MaLoaiQuangCao = query.MaLoaiQuangCao;
            result.TenLoaiQuangCao = query.TenLoaiQuangCao;

            return result;
        }

        public static void Insert(LoaiQuangCaoDTO _insert)
        {
            RaoVatDataContext dbRaoVat = new RaoVatDataContext();

            LOAIQUANGCAO loai = new LOAIQUANGCAO();
            loai.TenLoaiQuangCao = _insert.TenLoaiQuangCao;
            loai.Deleted = _insert.Deleted;

            dbRaoVat.LOAIQUANGCAOs.InsertOnSubmit(loai);
            dbRaoVat.SubmitChanges();
        }

        public static void Delete(int maLoai)
        {
            RaoVatDataContext dbRaoVat = new RaoVatDataContext();

            LOAIQUANGCAO _delete = (from n in dbRaoVat.LOAIQUANGCAOs
                                     where n.MaLoaiQuangCao == maLoai && n.Deleted == 0
                                     select n).Single();

            _delete.Deleted = 1;
            dbRaoVat.SubmitChanges();
        }

        public static void Update(LoaiNguoiDungDTO dto)
        {
            RaoVatDataContext dbRaoVat = new RaoVatDataContext();

            LOAIQUANGCAO _update = (from n in dbRaoVat.LOAIQUANGCAOs
                                     where n.MaLoaiQuangCao == dto.MaLoaiNguoiDung && n.Deleted == 0
                                     select n).Single();

            _update.TenLoaiQuangCao = dto.TenLoaiNguoiDung;

            dbRaoVat.SubmitChanges();
        }
    }
}
